<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/18
 * Time: 17:05
 */
namespace Admin\Controller;
use Think\QiNiuStorage;
class ServiceController extends AdminController{
    public function index()
    {
        $servicelist=M('service')->order('sort desc')->order('create_time desc')->select();
        $this->assign('_list',$servicelist);
        $this->meta_title='客服中心';
        $this->display();
    }

    public function add()
    {
        if(IS_POST){
            $service=M('service');

            $data = $service->create();

            $data || $this->error($service->getError());
            $data['create_time']=NOW_TIME;
            $id = $service->add($data);
            $id > 0 ? $this->success('新增成功', U('index')) : $this->error('新增失败');

        }else{
            $this->meta_title='添加客服';
            $this->display('add');
        }
    }

    public function edit()
    {
        $service=M('service');
        $id=I('id');
        if (IS_POST){
            $data = $service->create();
            $data || $this->error($service->getError());
            $status = $service->where(array('id'=>I('id')))->save();
            $status !== false ? $this->success('修改成功', U('index')) : $this->error('修改失败');
        }else{
            $list=$service->where(array('id'=>$id))->find();
            $this->assign('_list',$list);
            $this->display('add');
        }
    }

    public function remove()
    {
        $id = I('id');
        if (! $id) {
            $this->error('id不存在', Cookie('__forward__'));
        }
        $m_service = M("service");
        $result = $m_service->where(array(
            'id' => $id
        ))->delete();
        $result && $this->success('删除成功', U('index'));
        $this->error('删除失败', U('index'));
    }
}